$(function () {
    //接口搜索
    $("#fyl-search-btn").on('click', function () {
        setCookie("title", $("#searchTitle").val(), 1);
        location.href = '/api/list'
    })
    //修改密码
    $("#fyl-user").on('click', function () {
        var text = $(this).text();
        if (text == '登录') {
            verifyLogin();
        } else {
            layer.prompt({
                title: "<strong class='text-muted'>修改密码</strong>",
                formType: 1,
                maxlength: 255
            }, function (pass, index) {
                layer.close(index);
                ajaxFunParam("/xl/user/ajax/update/pwd", {"pwd": pass}, function (data) {
                    msgFunCallBack(data.msg, function () {
                        if (data.status == 0) {
                            location.href = "/api/list"
                        }
                    });
                })
            });
        }
    })
    //打开添加接口页面
    $("#fyl-insert").on('click', function () {
        //验证登录
        if (verifyLogin()) {
            openPageEnd($("#api-save"), function () {
                location.reload()
            })
        }
    })
    //绑定修改事件
    $(".fyl-update").on("click", function () {
        //验证登录
        if (verifyLogin()) {
            //接口id
            var id = $(this).data("id");
            $("#id").val(id);
            //接口名称
            var title = $("#title" + id).text();
            $("#title").val(title);
            //接口地址
            var url = $("#url" + id).text();
            $("#url").val(url);
            //请求方式
            var method = $("#method" + id).text();
            $("#method").val(method);
            //Json结果
            var jsonResult = $("#jsonResult" + id).text();
            $("#jsonResult").val(jsonResult);
            //接口说明
            var introduction = $("#introduction" + id).text();
            $("#introduction").val(introduction);
            //输入参数
            $("#inputAttr" + id + " tr").each(function (index, em) {
                console.log(index);
                var attrName = $(em).find(".attrName").text();
                var attrValue = $(em).find(".attrValue").text();
                var attrDesc = $(em).find(".attrDesc").text();
                var attrReq = $(em).find(".attrReq").text();
                updateInputAttr("inputAttr", index, {
                    "attrName": attrName,
                    "attrValue": attrValue,
                    "attrDesc": attrDesc,
                    "attrReq": attrReq
                })
            })
            insertInputAttrTr("inputAttr");
            //输出参数
            $("#outputAttr" + id + " tr").each(function (index, em) {
                var attrName = $(em).find(".attrName").text();
                var attrDesc = $(em).find(".attrDesc").text();
                updateOutputAttr("outputAttr", index, {"attrName": attrName, "attrDesc": attrDesc})
            })
            insertOutputAttrTr("outputAttr");
            //打开修改页面
            openPageEnd($("#api-save"), function () {
                location.reload()
            })
        }
    })
    //绑定删除事件
    $(".fyl-delete").on("click", function () {
        //验证登录
        if (verifyLogin()) {
            var id = $(this).data("id");
            delFun("/xl/api/ajax/delete", {"ids": id}, function (result) {
                msgFunCallBack(result.msg, function () {
                    location.reload();
                })
            })
        }
    })
    //初始化复制功能
    fuZhiBtnInit();
})

//复制功能初始化
function fuZhiBtnInit() {
    var clipboard = new ClipboardJS('.fyl-copy', {
        text: function (trigger) {
            return $(".fyl-copy-text-" + $(trigger).attr("data-id")).text();
        }
    });
    clipboard.on('success', function (e) {
        // console.log(e);
        layer.msg("复制成功 ~~~", {time: 1200, icon: 6});
    });
    clipboard.on('error', function (e) {
        // console.log(e);
        layer.msg("复制失败 ~~~", {time: 1200, icon: 5});
    });
}

//修改添加输入属性
function updateInputAttr(id, index, obj) {
    if (index == 0) {
        $("#" + id + " tr").find(".attrName").val(obj.attrName);
        $("#" + id + " tr").find(".attrValue").val(obj.attrValue);
        $("#" + id + " tr").find(".attrDesc").val(obj.attrDesc);
        $("#" + id + " tr").find(".attrReq").val(obj.attrReq);
    } else {
        var html = '<tr class="' + id + 'Item newItem"><td><input autocomplete="off" type="text" onkeydown="insertInputAttr(\'' + id + '\',this)" class="form-control ' + id + ' attrName" value="' + obj.attrName + '"></td><td><input autocomplete="off" type="text" onkeydown="insertInputAttr(\'' + id + '\',this)" class="form-control ' + id + ' attrValue" value="' + obj.attrValue + '"></td><td><input autocomplete="off" type="text" onkeydown="insertInputAttr(\'' + id + '\',this)" class="form-control ' + id + ' attrDesc" value="' + obj.attrDesc + '"></td><td><select class="form-control attrReq"><option value="否">否</option><option value="是" ' + (obj.attrReq == "是" ? "selected" : "") + '>是</option></select></td><td><a class="btn btn-danger" onclick="trNodeDel(this)">删除</a></td></tr>';
        $("#" + id).append(html);
    }
}

//修改添加输出属性
function updateOutputAttr(id, index, obj) {
    if (index == 0) {
        $("#" + id + " tr").find(".attrName").val(obj.attrName);
        $("#" + id + " tr").find(".attrDesc").val(obj.attrDesc);
    } else {
        var html = '<tr class="' + id + 'Item newItem"><td><input autocomplete="off" type="text" onkeydown="insertOutputAttr(\'' + id + '\',this)" class="form-control ' + id + ' attrName" value="' + obj.attrName + '"></td><td><input autocomplete="off" type="text" onkeydown="insertOutputAttr(\'' + id + '\',this)" class="form-control ' + id + ' attrDesc" value="' + obj.attrDesc + '"></td><td><a class="btn btn-danger" onclick="trNodeDel(this)">删除</a></td></tr>';
        $("#" + id).append(html);
    }
}